/*
 * 文件名： FavorActivity.java
 * 描    述：我的收藏
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */
package com.miguo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.miguo.activity.base.BaseActivity;
import com.miguo.activity.fragment.FavorFragment;
import com.miguo.component.BaseHeadView;
import com.miguo.framework.view.pagerindicator.TabPageIndicator;
import com.miguo.framework.view.pagerindicator.UnderlinePageIndicator;
import com.miguo.ui.R;


public class FavorActivity extends BaseActivity {

    private BaseHeadView mBaseHead;
    private ViewPager mPagerView;
    private TabPageIndicator mTabPageIndicator;
    private UnderlinePageIndicator mLineIndicator;
    private String[] titles = {"全部", "开抢提醒", "抢购中"};
    private int position = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favor);
        initExtra();
        initView();
    }

    private void initExtra() {
        Intent in = getIntent();
        position = in.getIntExtra("position", 0);
        if (in.hasExtra("favor")) {
            isBack = in.getBooleanExtra("favor", true);
        }
    }

    private void initView() {
        mBaseHead = (BaseHeadView) findViewById(R.id.base_head);
        mBaseHead.setActivity(this);
        mBaseHead.setBackBtn(isBack);
        mBaseHead.setTitle(R.string.favor_title);

        mPagerView = (ViewPager) findViewById(R.id.pager_view);
        FragmentPagerAdapter mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
        mPagerView.setAdapter(mPagerAdapter);

        mLineIndicator = (UnderlinePageIndicator) findViewById(R.id.line_indicator);
        mLineIndicator.setViewPager(mPagerView);

        //得到指示器
        mTabPageIndicator = (TabPageIndicator) findViewById(R.id.tab_indicator);
        mTabPageIndicator.setViewPager(mPagerView);
        mTabPageIndicator.setOnPageChangeListener(mLineIndicator);
        mTabPageIndicator.setCurrentItem(position);

    }

    private class PagerAdapter extends FragmentPagerAdapter {

        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return FavorFragment.newInstance(position);
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        //顶部的title
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position % titles.length].toUpperCase();
        }
    }
}
